var _HAS_SET_MASK_DIV_4LOOKUP=false;

function LookupComponent(_schema)
{
	this.value={id:"",name:"",code:""};
	this.lookupping=false;
	this.schema=adjustSchema(_schema);
	this.identities={textInputId:this.schema.id+"_text_4lookup",
						       addButtonId:this.schema.id+"_add_button_4lookup",
						       searchButtonId:this.schema.id+"_search_button_4lookup",
						       lookupDivId:this.schema.id+"_lookup_div",
						       lookupFrameId:this.schema.id+"_lookup_frame"};
	this.elements=createElements(this);
	
	this.showLookup=function()
	{
		 this.elements.lookupDiv.show();
		 this.lookupping=true;
	};
	
	this.hideLookup=function()
	{
		 this.elements.lookupDiv.hide();
		 this.lookupping=false;
	};
	
	this.addModel=function()
	{
		this.elements.lookupFrame.attr("src",this.schema.addUrl);
    this.showLookup();
	};
	
	this.searchModel=function()
	{
		this.elements.lookupFrame.attr("src",this.schema.searchUrl);
    this.showLookup();
	};
	
	//检测时否输入值有变化
  this.checkLookup=function()
	{
		if(this.elements.textInput.val()=="")
	  {
	  	this.value={id:"",name:"",code:""};
	  	this.elements.textInput.attr("history","");
		  this.hideLookup();
	  }
		else if(this.elements.textInput.attr("history")!==this.elements.textInput.val())
		{
			this.elements.textInput.attr("history",this.elements.textInput.val());
			var _lookupUrl=this.schema.lookupUrl+this.schema.lookupKeyword+"="+encodeURI(this.elements.textInput.val());
  		this.elements.lookupFrame.attr("src",_lookupUrl);
  		this.showLookup();
		}
	};

  this.initValue=function(data)
  {
  	if(data)
  	{
	  	this.value=data;
			this.elements.textInput.val(data.name);
			this.schema.lookupResult(data);
		}
  };
	
	this.resetValue=function(data)
	{
  	if(data)
  	{
	  	this.value=data;
			this.elements.textInput.val(data.code);
			this.elements.textInput.focus();
			this.schema.lookupResult(data);
	 	}
	};
	
	//增加look up组件到容器
	this.appendSelf=function(_parement)
	{
	  _parement=jQuery(_parement);
	  _parement.append(this.elements.scripts);
		_parement.append(this.elements.label);
    _parement.append(this.elements.textInput);
    	if(this.elements.addButton)
    	{
			_parement.append(this.elements.addButton);
		}
		if(this.elements.searchButton)
		{
			_parement.append(this.elements.searchButton);
		}
		
		var _body=jQuery("body");
	  //如果没有设置mask div，新增mask_div
	  if(!_HAS_SET_MASK_DIV_4LOOKUP)
	  {	
	  	var _component=jQuery(this);
	  	var _maskDiv=jQuery("<div></div>");
			_maskDiv.css("display","block");
			_maskDiv.css("position","absolute");
			_maskDiv.css("left","0");
			_maskDiv.css("top","0");
			_maskDiv.css("z-index","-100");
			_maskDiv.css("width","100%");
			_maskDiv.css("min-width","1000px");
			_maskDiv.css("height","100%");
			_maskDiv.css("min-height","900px");
			_maskDiv.css("background-color","#FFF");
			_maskDiv.click(function(event)
			{
				//_component.elements.textInput.focus();
				hideAllLookup(_component);
		  });
	  	_body.append(_maskDiv);
	  	_HAS_SET_MASK_DIV_4LOOKUP=true;
	  }
	  this.elements.lookupDiv.css("position","absolute");
		this.elements.lookupDiv.css("left",this.elements.textInput.offset().left);
	  this.elements.lookupDiv.css("top",this.elements.textInput.offset().top+this.elements.textInput.outerHeight());
	  this.elements.lookupDiv.hide();
	  _body.append(this.elements.lookupDiv);
	};
	
	function createElements(_component)
	{
		var _label="<span>"+_component.schema.label+" </span>";
		
		var _lookupFrame=jQuery("<iframe id='"+_component.identities.lookupFrameId+"' name='"+_component.identities.lookupFrameId+"'/>");
		_lookupFrame.css("index","999");
		_lookupFrame.css("height","100%");
		_lookupFrame.css("width","100%");
		_lookupFrame.css("margin","2px");
		_lookupFrame.ready(function()
		{ 
     	jQuery('body', jQuery('iframe').contents()).html("页中面内容加载......"); 
    }); 
    _lookupFrame.load(function()
    {
      var lookupFrame=self.frames[_component.identities.lookupFrameId];
    	if(lookupFrame._LOOKUPCONTROL)
    	{
      	lookupFrame._LOOKUPCONTROL.setComponent(_component);
      }
    });
    
		var _lookupDiv=jQuery("<div></div>");
		_lookupDiv.attr("id",_component.identities.lookupDivId);
		//标示所有的lookup div
		_lookupDiv.attr("lookupDiv","true");
		_lookupDiv.css("display","none");
		_lookupDiv.css("index","100");
		_lookupDiv.css("width","500");
		_lookupDiv.css("height","300");
		_lookupDiv.css("background-color","#ccc");
		_lookupDiv.append(_lookupFrame);
		
		var _input=jQuery("<input type='text'>");
		_input.attr("id",_component.identities.textInputId);
		_input.attr("name",_component.schema.name);
		_input.attr("history",_component.value.code);
		_input.val(_component.value.code);
		
		_input.blur(function(e){
			if(!_component.lookupping)
			{
	      var _this=jQuery(this);
	      _this.val(_component.value.name);
	      if(_this.val()=="")
	      {
	      	_this.attr("history","");
	      }
	    }
    });
    _input.focus(function(e){ 
    	var _this=jQuery(this);
      _this.val(_component.value.code);
      hideAllLookup(_component);
    });
		_input.keyup(function(e)
		{
			_component.checkLookup();
			var lookupFrame=self.frames[_component.identities.lookupFrameId];
			//检测上，下，回车，ESC
      if(_component.lookupping)
      {
      	switch(e.keyCode)
      	{
	      	case 38: //向上键
      			if(lookupFrame._LOOKUPCONTROL)
      			{	
      				lookupFrame._LOOKUPCONTROL.gotoPrevious();
      			}
	      		break;
			  	case 40: //向下
			  		if(lookupFrame._LOOKUPCONTROL)
      			{	
      				lookupFrame._LOOKUPCONTROL.gotoNext();
      			}
	    	    break;
	    	  case 13: //回车
	    	  	if(lookupFrame._LOOKUPCONTROL)
      			{	
	   					_component.resetValue(lookupFrame._LOOKUPCONTROL.returnValue());
	   				}
	    	    break;
	    	  case 27: //ESC
	   		    hideAllLookup(_component);
	   		    break;
	   		}
   		};
	  });
	  
	  var _addBtn=null;
	  if(_component.schema.addUrl && _component.schema.addUrl!="")
	  {
	  	_addBtn=jQuery("<input type='button'>");
			_addBtn.attr("id",_component.identities.addButtonId);
			_addBtn.attr("value","+");
			_addBtn.click(function(event)
			{
				_component.addModel();
			});
	  }
	  var _searchBtn=null;
	  if(_component.schema.searchUrl && _component.schema.searchUrl!="")
	  {
			_searchBtn=jQuery("<input type='button'>");
			_searchBtn.attr("id",_component.identities.searchButtonId);
			_searchBtn.attr("value","...");
			_searchBtn.click(function(event)
			{
				_component.searchModel();
			});
		}
		return {label:_label,lookupDiv:_lookupDiv,lookupFrame:_lookupFrame,textInput:_input,addButton:_addBtn,searchButton:_searchBtn};
	}

	function hideAllLookup(_component)
	{
		jQuery('div[lookupDiv=true]').hide();
		_component.lookupping=false;
		
	}
	
	function adjustSchema(_schema)
	{
		var theSchema=jQuery.extend({},LookupComponent.Schema,_schema);
		if(theSchema.lookupUrl)
		{
			theSchema.lookupUrl=adjustUrl(theSchema.lookupUrl);
		}
		/*
		if(theSchema.searchUrl)
		{
			theSchema.searchUrl=adjustUrl(theSchema.searchUrl);
		}*/
		return theSchema;
	}

	
	function adjustUrl(url)
	{
		var _charQ=url.lastIndexOf("?");
		if(_charQ>0)
		{
			var _last=url.length-1;
			if(_charQ==_last)
			{
			  return url;
			}
			var _charA=url.lastIndexOf("&");
			if(_charA==_last)
			{
				return url;
			}
			else
			{
				return url+"&";	
			}
		}
	  else
		{
			return url+"?";
		}
		
	}
}


LookupComponent.Schema=
{
	id:'company',
	name:'company',
	label:'公司',
	addUrl:'',
	lookupUrl:'',
	searchUrl:'',
	lookupResult:function(data){ },//lookup后的结果数据处理方法，data基本的格式为({id:1,name:ABC,code:123});
	lookupKeyword:'q'
};
